const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

const formatTime_1 = date => {


  let str = date;
  //将字符串转换成时间格式
  let timePublish = new Date(str);
  let timeNow = new Date();
  let minute = 1000 * 60;
  let hour = minute * 60;
  let day = hour * 24;
  let month = day * 30;
  let year = month * 12;
  let diffValue = timeNow - timePublish;
  let diffMonth = diffValue / month;
  let diffWeek = diffValue / (7 * day);
  let diffDay = diffValue / day;
  let diffHour = diffValue / hour;
  let diffMinute = diffValue / minute;
  let diffYear = diffValue / year;
  let result = null
  
  if (diffValue >= 0 && diffValue<=179866) {
    result = "刚刚";
  } else if (diffMinute < 60&&diffValue>0) {
    result = parseInt(diffMinute) + " 分钟前";
  } else if (diffHour < 24) {
    result = parseInt(diffHour) + " 小时前";
  } else if (diffDay >1 && diffDay < 7) {
    //几天前
    return `${parseInt(diffDay) }天前`
  } else  {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    // //判断是否为今年
    const isThisYear = year === new Date().getFullYear()
    // //判断是否为今天
    // const isToday = isThisYear && month === new Date().getMonth() + 1 && day === new Date().getDate()
    // //判断是否为昨天
    // const isYesterday = isThisYear && month === new Date().getMonth() + 1 && day === new Date().getDate() - 1
    // const isToday_4 = isThisYear && month === new Date().getMonth() + 1 && day === new Date().getDate() - 1 && hour === new Date().getHours() && minute === new Date().getMinutes()
    // if (isYesterday) {
    //   //     //昨天
    //   return `昨天 ${hour}:${minute}`
    // } else if (isToday_4) {
    //   //几天前
    //   return `${day}天前`
   if (isThisYear) {
      //今年
      return `${month}-${day} ${hour}:${minute}`
    } else {
    
    return `${year}-${month}-${day} ${hour}:${minute}`
    }

  }





  //   const year = date.getFullYear()
  //   const month = date.getMonth() + 1
  //   const day = date.getDate()
  //   const hour = date.getHours()
  //   const minute = date.getMinutes()
  //   const second = date.getSeconds()
  //   //判断是否为今年
  //   const isThisYear = year === new Date().getFullYear()
  //   //判断是否为今天
  //   const isToday = isThisYear && month === new Date().getMonth() + 1 && day === new Date().getDate()
  //   //判断是否为昨天
  //   const isYesterday = isThisYear && month === new Date().getMonth() + 1 && day === new Date().getDate() - 1
  //  //判断是否为刚刚
  //   const isToday_1 = isThisYear && month === new Date().getMonth() + 1 && day === new Date().getDate() && hour === new Date().getHours() && minute === new Date().getMinutes()
  //   //判断是否为几分钟前
  //   const isToday_2 = isThisYear && month === new Date().getMonth() + 1 && day === new Date().getDate() && hour === new Date().getHours() && minute === new Date().getMinutes() - 1
  //   //判断是否为几小时前
  //   const isToday_3 = isThisYear && month === new Date().getMonth() + 1 && day === new Date().getDate() && hour === new Date().getHours() - 1 && minute === new Date().getMinutes()
  //   //判断是否为几天前
  //   const isToday_4 = isThisYear && month === new Date().getMonth() + 1 && day === new Date().getDate() - 1 && hour === new Date().getHours() && minute === new Date().getMinutes()
  //   if (isToday_1) {
  //     //刚刚
  //     return '刚刚'
  //   } else if (isToday_2) {
  //     //几分钟前
  //     return `${minute}分钟前`
  //   } else if (isToday_3) {
  //     //几小时前
  //     return `${hour}小时前`
  //   }  else if (isToday) {
  //     //今天
  //     return `今天 ${hour}:${minute}`
  //   } else if (isYesterday) {
  //     //昨天
  //     return `昨天 ${hour}:${minute}`
  //   } 
  //   else if (isToday_4) {
  //     //几天前
  //     return `${day}天前`
  //   }
  //   else if (isThisYear) {
  //     //今年
  //     return `${month}月${day}日 ${hour}:${minute}`
  //   } else {
  //     //其他
  //     return `${year}年${month}月${day}日 ${hour}:${minute}`
  //   }
  // if(isThisYear){
  //   return `${month}月${day}日 ${hour}:${minute}`
  // }else{
  //   return `${year}年${month}月${day}日 ${[hour, minute].map(formatNumber).join(':')}`
  // }

  return result
}

module.exports = {
  formatTime,
  formatTime_1
}